package com.example.xpagedemoproject.fragment;

import android.content.Intent;
import android.os.Bundle;

import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.base.XPageSimpleListFragment;
import com.xuexiang.xpage.core.PageOption;
import com.xuexiang.xutil.tip.ToastUtils;

import java.util.List;

@Page(name = "数据传递")
public class DataTransmitFragment extends XPageSimpleListFragment {
    public static final String TAG = "DataTransmitFragment";
    @Override
    protected List<String> initSimpleData(List<String> lists) {
        lists.add("数据传递(不返回值)");
        lists.add("数据传递(带返回值)");
        return lists;
    }

    @Override
    protected void onItemClick(int position) {
        Bundle bundle = new Bundle();
        switch (position) {
            case 0:
                bundle.putBoolean(DataReceiveFragment.KEY_IS_NEED_BACK,false);
                int id = (int) (Math.random() * 100);
                bundle.putString(DataReceiveFragment.KEY_EVENT_NAME,"事件" + id);
                bundle.putString(DataReceiveFragment.KEY_EVENT_DATA,"事件" + id + "携带的数据");
                openPage(DataReceiveFragment.class,bundle);
                break;
            case 1:
                PageOption.to(DataReceiveFragment.class)
                        .putBoolean(DataReceiveFragment.KEY_IS_NEED_BACK,true)
                        .setRequestCode(100)
                        .open(this);
                break;
            default:
                break;
        }

    }

    @Override
    public void onFragmentResult(int requestCode, int resultCode, Intent data) {
        super.onFragmentResult(requestCode, resultCode, data);
        if (data != null) {
            Bundle bundle = data.getExtras();
            ToastUtils.toast(String.format("requestCode:%d\nresultCode:%d\n%s",
                    requestCode,
                    resultCode,
                    bundle.getString(DataReceiveFragment.KEY_EVENT_DATA)));
        }
    }
}
